package org.jeecg.modules.wangyue.controller.sys;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.CommonConstant;
import org.jeecg.modules.wangyue.constant.DelFlagConstant;
import org.jeecg.modules.wangyue.constant.RoleConstant;
import org.jeecg.modules.wangyue.entity.IndentJs;
import org.jeecg.modules.wangyue.entity.UserRoleInfo;
import org.jeecg.modules.wangyue.service.IIndentJsService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.wangyue.service.IUserRoleInfoService;
import org.jeecg.modules.wangyue.utils.ExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 订单管理
 * @Author: jeecg-boot
 * @Date:   2023-06-30
 * @Version: V1.0
 */
@Slf4j
@Api(tags="订单管理")
@RestController
@RequestMapping("/wangyue/sys/indentManage")
public class IndentManageController extends JeecgController<IndentJs, IIndentJsService> {
	@Autowired
	private IIndentJsService indentJsService;
	@Autowired
	private IUserRoleInfoService userRoleInfoService;
	@Autowired
	private ExportUtil exportUtil;


	 /**
	  * 分页列表查询
	  * @param keywords		关键字
	  * @param orderStatus	订单状态
	  * @param beginTime	开始时间
	  * @param endTime		结束时间
	  * @param pageNo		第几页
	  * @param pageSize		每页大小
	  * @return
	  */
	@AutoLog(value = "订单管理-分页列表查询")
	@ApiOperation(value="订单管理-分页列表查询", notes="订单管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(@RequestParam(name="keywords", required = false) String keywords,
								   @RequestParam(name="orderStatus", required = false) String orderStatus,
								   @RequestParam(name="beginTime", required = false) String beginTime,
								   @RequestParam(name="endTime", required = false) String endTime,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		QueryWrapper<IndentJs> queryWrapper = new QueryWrapper<>();
		if(oConvertUtils.isNotEmpty(keywords)){
			queryWrapper.like("order_no", keywords);
		}
		if(oConvertUtils.isNotEmpty(orderStatus)){
			queryWrapper.eq("order_status", orderStatus);
		}
		if(oConvertUtils.isNotEmpty(beginTime) && oConvertUtils.isNotEmpty(endTime)){
			queryWrapper.apply("create_time >= '" + beginTime + "' and create_time <= '" + endTime + "'");
		}
		queryWrapper.eq("del_flag", DelFlagConstant.normal);
		queryWrapper.orderByDesc("create_time");
		Page<IndentJs> page = new Page<IndentJs>(pageNo, pageSize);
		IPage<IndentJs> pageList = indentJsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	 /**
	  * 管理端-查询技师数据
	  * @param jsUserId 技师id
	  * @return
	  */
	 @AutoLog(value = "管理端-查询技师数据")
	 @ApiOperation(value="管理端-查询技师数据", notes="管理端-查询技师数据")
	 @GetMapping(value = "/findJsUserData")
	 public Result<?> findJsUserData(@RequestParam(name="jsUserId", required = true) String jsUserId) {
	 	 QueryWrapper<UserRoleInfo> queryWrapper = new QueryWrapper<>();
	 	 queryWrapper.eq("user_id", jsUserId);
	 	 queryWrapper.eq("user_type", RoleConstant.jishi);
		 queryWrapper.eq("del_flag", DelFlagConstant.normal);
	 	 return Result.OK(userRoleInfoService.getOne(queryWrapper));
	 }




  /**
   * 导出excel
   *
   * @param indentJs
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(IndentJs indentJs) {
	  QueryWrapper<IndentJs> queryWrapper = new QueryWrapper<>();
	  if(oConvertUtils.isNotEmpty(indentJs.getIds())){
	  	queryWrapper.in("id", Arrays.asList(indentJs.getIds().split(",")));
	  }
	  if(oConvertUtils.isNotEmpty(indentJs.getKeywords())){
		  queryWrapper.like("order_no", indentJs.getKeywords());
	  }
	  if(oConvertUtils.isNotEmpty(indentJs.getOrderStatus())){
		  queryWrapper.eq("order_status", indentJs.getOrderStatus());
	  }
	  if(oConvertUtils.isNotEmpty(indentJs.getBeginTime()) && oConvertUtils.isNotEmpty(indentJs.getEndTime())){
		  queryWrapper.apply("create_time >= '" + indentJs.getBeginTime() + "' and create_time <= '" + indentJs.getEndTime() + "'");
	  }
	  queryWrapper.eq("del_flag", DelFlagConstant.normal);
	  queryWrapper.orderByDesc("create_time");
	  List<IndentJs> indentJsList = indentJsService.list(queryWrapper);

	  //处理url数据，添加文件地址前缀
	  if(oConvertUtils.isNotEmpty(indentJsList) && indentJsList.size() > 0){
		  for(IndentJs ij : indentJsList){
			  if (oConvertUtils.isNotEmpty(ij.getSignInPicUrl())){
				  ij.setSignInPicUrl(CommonConstant.fileAddressPrefix + ij.getSignInPicUrl());
			  }
			  if (oConvertUtils.isNotEmpty(ij.getSignInVideoUrl())){
				  ij.setSignInVideoUrl(CommonConstant.fileAddressPrefix + ij.getSignInVideoUrl());
			  }
			  if (oConvertUtils.isNotEmpty(ij.getFinishServicePicUrl())){
				  ij.setFinishServicePicUrl(CommonConstant.fileAddressPrefix + ij.getFinishServicePicUrl());
			  }
			  if (oConvertUtils.isNotEmpty(ij.getFinishServiceVideoUrl())){
				  ij.setFinishServiceVideoUrl(CommonConstant.fileAddressPrefix + ij.getFinishServiceVideoUrl());
			  }
		  }
	  }

      return exportUtil.exportXls(indentJsList, IndentJs.class, "技师订单数据");
  }


}
